package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.exception.Business40Exception;
import com.changgou.entity.exception.EResultCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by 柱子 on 2020/10/21 21:09
 */
@Service
public class ESManagerServiceImpl implements ESManagerService {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    // @Resource
    private SkuFeign skuFeign;

    @Autowired
    private ESManagerMapper esManagerMapper;

    //创建索引库结构
    @Override
    public void createIndexAndMapping() {

        //创建索引
        elasticsearchTemplate.createIndex(SkuInfo.class);

        //创建映射
        elasticsearchTemplate.putMapping(SkuInfo.class);

    }

    //导入全部Sku集合到索引库
    @Override
    public void importAll() {
        //查询Sku集合
        //  List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        if (skuList == null || skuList.size() <= 0) {
            //查询错误
            throw new RuntimeException("当前数据未查询到");
        }
        //skuList转换为Json
        String jsonSkuList = JSON.toJSONString(skuList);
        //将Json转换为SkuInfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息转换为map
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        //导入索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    //根据SpuId查询SkuList，添加到索引库
    @Override
    public void importDataToESBySpuId(String spuId) {
        List<Sku> skuListBySpuId = skuFeign.findSkuListBySpuId(spuId);

        if (skuListBySpuId == null || skuListBySpuId.size() <= 0) {
            //查询错误
            throw new Business40Exception(EResultCode.FEIGN_DATA_NO);
        }

        //将集合转换为Json
        String jsonSkuList = JSON.toJSONString(skuListBySpuId);

        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);

        for (SkuInfo skuInfo : skuInfoList) {
            //将规格信息进行转换
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }
        //添加到索引库
        esManagerMapper.saveAll(skuInfoList);
    }

    /**
     * 根据ID删除SkuList索引库
     *
     * @param spuId
     */
    @Override
    public void delDataBySpuId(String spuId) {

        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            //查询错误
            throw new RuntimeException("当前数据未查询到");
        }
        //删除
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.parseLong(sku.getId()));
        }


    }
}
